<template>
  <div class="container">
    <!-- 搜索框 -->
    <div class="search-box">
      <el-input
        v-model="searchKeyword"
        :placeholder="getPlaceholder"
        class="search-input"
        clearable
      >
      </el-input>
      <el-button type="primary" @click="handleSearch">搜索</el-button>
    </div>

        <!-- 工厂筛选条件 -->
    <div class="filter-section">
      <span class="filter-label">类型1：</span>
      <el-select v-model="factoryFilters.type1" placeholder="请选择">
        <el-option label="条件1" value="1" />
        <el-option label="条件2" value="2" />
        <el-option label="条件3" value="3" />
      </el-select>
      
    </div>

      <!-- 工厂列表 -->
    <el-card v-for="item in pageList" :key="item.factoryId" class="company-card"  @click="goToFactoryDetail(item.userId)">
      <div class="company-info">
        <h3>{{ item.factoryName }}</h3>
        <p>{{ item.description }}</p>
        <div class="company-meta">
          <span>售卖类型:{{ item.type }}</span>
          <span>{{ item.location }}</span>
        </div>
      </div>
    </el-card>
  </div>

  
          <!--分页区域-->
      <el-pagination
        class="pagination"
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[6]"
        :small="small"
        :disabled="disabled"
        :background="background"
        layout="sizes,prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        style="position: absolute ;bottom: 30px; margin-top: 30px; margin-left: 20%;"
        />
</template>

<script setup>
import { ref,onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { searchFactoryApi ,searchFactoryByNameApi } from '@/api/search'

// 搜索关键词
const searchKeyword = ref('')
// 工厂筛选条件
const factoryFilters = ref({
  type1: ''
})

const factoryList = ref([])
// const factoryList = ref([
//   {
//     id: 1,
//     name: '桃褶服装股份有限公司',
//     description: '专业服装加工制造',
//     location: '贵州省贵阳市花溪区清溪路',
//     capacity: '月产10000件',
//     rating: 4.8
//   },
//     {
//     id: 1,
//     name: '桃褶服装股份有限公司',
//     description: '专业服装加工制造',
//     location: '贵州省贵阳市花溪区清溪路',
//     capacity: '月产10000件',
//     rating: 4.8
//   },
//   // ... 更多数据
// ])

const pageList = ref([]); // 当前页的数据（用于显示）

const getFactoryList = async () => {
  const res = await searchFactoryByNameApi('')
  factoryList.value = res.data
  total.value = factoryList.value.length
  pagePages.value = Math.ceil(total.value / pageSize.value); // 计算总页数
  updatePaginatedList();   // 更新分页数据
}
onMounted(() => {
  getFactoryList()
})

// 搜索处理
const handleSearch = async () => {
  const res = await searchFactoryByNameApi(searchKeyword.value)
  factoryList.value = res.data
  total.value = factoryList.value.length
  pagePages.value = Math.ceil(total.value / pageSize.value); // 计算总页数
  updatePaginatedList();   // 更新分页数据
}

//分页
const total = ref(0); // 总条数
const pagePages = ref(1); // 总页数
const currentPage = ref(1); // 当前页码
const pageSize = ref(6);  // 每页显示条数

// 更新分页数据（根据 currentPage 和 pageSize）
const updatePaginatedList = () => {
  const start = (currentPage.value - 1) * pageSize.value;
  const end = start + pageSize.value;
  pageList.value = factoryList.value.slice(start, end);  // 切割出当前页的数据
};

// 处理每页条数变化
const handleSizeChange = (newSize) => {
  pageSize.value = newSize; // 更新每页显示的条目数
  updatePaginatedList();  // 更新分页数据
};

// 处理当前页码变化
const handleCurrentChange = (newPage) => {
  currentPage.value = newPage; // 更新当前页码
  updatePaginatedList();  // 更新分页数据
};


// 跳转详情
const router = useRouter()
// 跳转工厂详情
const goToFactoryDetail = (id) => {
  const routeData = router.resolve({
    name: 'factoryDetail',
    params: { userId: id }
  })
  // 在新窗口打开
  window.open(routeData.href, '_blank')
}

</script>

<style scoped lang="scss">
.container{
  min-height: 100vh;
}
.search-box {
  max-width: 600px;
  margin: 0 auto 20px;
  display: flex;
  .search-input {
    width: 100%;
  }
  .el-button {
    margin-left: 20px;
    width: 80px;
  }
}

.filter-section {
  margin: 20px 0;
  display: flex;
  align-items: center;
  .el-select {
    width: 150px;
  }
}

.filter-label {
  margin-right: 10px;
  color: #606266;
}

.filter-tag {
  margin-right: 10px;
  cursor: pointer;
}

.company-card {
  margin-bottom: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  
  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
  }
}

.company-meta {
  display: flex;
  align-items: center;
  gap: 20px;
  margin-top: 10px;
  color: #909399;
}

/* 响应式布局 */
@media screen and (max-width: 1200px) {
  .el-col {
    width: 25%;
  }
}

@media screen and (max-width: 992px) {
  .el-col {
    width: 33.33%;
  }
}

@media screen and (max-width: 768px) {
  .el-col {
    width: 50%;
  }
}

@media screen and (max-width: 576px) {
  .el-col {
    width: 100%;
  }
}
</style>